﻿namespace Dcms.BaseClass
{
    using Dcms.ToEmpower;
    using System;
    using System.Data;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web.UI;

    public abstract class DcmsTempBaseResolve
    {
        public DcmsTempBaseResolve()
        {
            Dcms.ToEmpower.ToEmpower.ValidateAndRedirect();
        }

        protected bool CheckIsNextLevel(string CheckItemStyle, string RegStr)
        {
            Regex myregx = new Regex(RegStr);
            return myregx.Matches(CheckItemStyle).Count > 0;
        }

        public string DcmsTempLateResolve(string ModelName, DataRow Datadr, string HtmlTmep, int ItemIndexId)
        {
            string TempColumnName = "";
            string TempColumnReg = "";
            foreach (DataColumn ItemColumns in Datadr.Table.Columns)
            {
                TempColumnName = "{" + FormatFile(ItemColumns.ColumnName) + "}";
                HtmlTmep = Regex.Replace(HtmlTmep, TempColumnName, Datadr[ItemColumns.ColumnName].ToString(), RegexOptions.IgnoreCase);
                TempColumnReg = "{" + FormatFile(ItemColumns.ColumnName) + @",(?<Len>(\d*)?)}";
                HtmlTmep = this.FarmatStringLenght(TempColumnReg, HtmlTmep, Datadr[ItemColumns.ColumnName].ToString());
                if (ItemColumns.DataType.ToString() == "System.DateTime")
                {
                    TempColumnReg = "{" + FormatFile(ItemColumns.ColumnName) + ",(?<Style>([^}]*)?)}";
                    HtmlTmep = this.FarmatTimeStr(TempColumnReg, HtmlTmep, Datadr[ItemColumns.ColumnName].ToString());
                }
            }
            TempColumnName = "{dcms.indexid}";
            HtmlTmep = Regex.Replace(HtmlTmep, TempColumnName, ItemIndexId.ToString(), RegexOptions.IgnoreCase);
            string RegexStr = "{" + ModelName + @".(?<FileName>(\w+))\.(\w+),?(?<len>(\w*?))}";
            if (GetNextLevelControlsCount(HtmlTmep, RegexStr) > 0) HtmlTmep = this.FormatSpecialFile(ModelName, HtmlTmep, Datadr);
            return HtmlTmep;
        }

        private string FarmatStringLenght(string RegexStr, string HtmlTempLate, string NewVlaue)
        {
            MatchCollection matches = new Regex(RegexStr, RegexOptions.IgnoreCase).Matches(HtmlTempLate);
            if (matches.Count > 0)
            {
                Regex Myreg2 = new Regex(RegexStr, RegexOptions.IgnoreCase);
                foreach (Match m in matches)
                {
                    HtmlTempLate = Myreg2.Replace(HtmlTempLate, this.FormatStrLength(NewVlaue, m.Groups["Len"].Value));
                }
            }
            return HtmlTempLate;
        }

        private string FarmatTimeStr(string RegexStr, string HtmlTempLate, string NewVlaue)
        {
            MatchCollection mymatches = new Regex(RegexStr, RegexOptions.IgnoreCase).Matches(HtmlTempLate);
            if (mymatches.Count > 0)
            {
                foreach (Match m in mymatches)
                {
                    HtmlTempLate = Regex.Replace(HtmlTempLate, RegexStr, this.FormatDataTime(NewVlaue, m.Groups["Style"].Value), RegexOptions.IgnoreCase);
                }
            }
            return HtmlTempLate;
        }

        public abstract string FormatDataTime(string DataTimeStr, string StyleStr);
        private static string FormatFile(string FileName)
        {
            return (FileName = FileName.Replace('_', '.'));
        }

        public abstract string FormatSpecialFile(string ModelName, string HtmlTmep, DataRow Datadr);
        public abstract string FormatStrLength(string Str, string LenghtStr);
        public abstract string FormatStrProcess(string CurrIndex, int Step);
        public abstract string GetHtmlTempStr(string Htmlstr, ControlCollection Controls);
        protected static string GetModelName(string ConditionStr, string RegexStr, string StrName)
        {
            MatchCollection mymath = new Regex(RegexStr, RegexOptions.IgnoreCase).Matches(ConditionStr);
            if (mymath.Count > 0) return mymath[0].Groups[StrName].Value;
            return "";
        }

        protected static int GetNextLevelControlsCount(string StyleItmestr, string RegStr)
        {
            Regex myregx = new Regex(RegStr);
            return myregx.Matches(StyleItmestr).Count;
        }

        public abstract DataTable GetSqlDataTabel(string SqlStr);
        public string ResolveHtmlModel(int ChildControlsCount, string NextLevel, string NextNextLeval, string CurrLevelStyleStr, string CurrLevelFormatSql, ControlCollection Controls)
        {
            StringBuilder MyResolveHtmlSb = new StringBuilder();
            string TempCurrLevelModelName = "";
            string TempCurrLevelStyleStr = "";
            int CurrLevelStyleItemNum = 0;
            string[] splitStr = new string[] { "$dcmsItem$" };
            DataTable CurrLevelDataTable = new DataTable();
            for (int i = 0; i < ChildControlsCount; i++)
            {
                TempCurrLevelModelName = CurrLevelFormatSql.Split(new char[] { '$' })[2];
                string[] TempCurrLevelStyleArr = CurrLevelStyleStr.Split(splitStr, StringSplitOptions.RemoveEmptyEntries);
                CurrLevelStyleItemNum = TempCurrLevelStyleArr.Length;
                CurrLevelDataTable = this.GetSqlDataTabel(CurrLevelFormatSql);
                if (CurrLevelDataTable.Rows.Count > 0)
                {
                    for (int j = 0; j < CurrLevelDataTable.Rows.Count; j++)
                    {
                        if (CurrLevelStyleItemNum > 1)
                        {
                            if (j < CurrLevelStyleItemNum)
                                TempCurrLevelStyleStr = TempCurrLevelStyleArr[j].ToString();
                            else
                            {
                                int TempIndex = j % CurrLevelStyleItemNum;
                                TempCurrLevelStyleStr = TempCurrLevelStyleArr[TempIndex].ToString();
                            }
                        }
                        else
                            TempCurrLevelStyleStr = TempCurrLevelStyleArr[0].ToString();
                        if (this.CheckIsNextLevel(TempCurrLevelStyleStr, "Dcms_Label_" + NextNextLeval + @"(\d+)"))
                        {
                            TempCurrLevelStyleStr = this.DcmsTempLateResolve(TempCurrLevelModelName, CurrLevelDataTable.Rows[j], TempCurrLevelStyleStr, j + 1);
                            string NextLevelStyleStr = "";
                            string LooPTempResolveHtml = "";
                            string NextLevelFileList = "";
                            int TempNextNextChildCount = 0;
                            int ReplaceNextNextId = 0;
                            TempNextNextChildCount = GetNextLevelControlsCount(TempCurrLevelStyleStr, "Dcms_Label_" + NextNextLeval + @"(\d+)");
                            for (int k = 0; k < TempNextNextChildCount; k++)
                            {
                                ReplaceNextNextId = j % CurrLevelStyleItemNum * TempNextNextChildCount + k;
                                NextLevelStyleStr = this.GetHtmlTempStr(string.Concat(new object[] { "Dcms_Label_", NextNextLeval, ReplaceNextNextId, "StyleLevel" }), Controls);
                                NextLevelFileList = this.GetHtmlTempStr(string.Concat(new object[] { "Dcms_Label_", NextNextLeval, ReplaceNextNextId, "FormatSql" }), Controls);
                                string[] NextLavelFileArr = NextLevelFileList.Split(new char[] { '$' });
                                NextLevelFileList = this.DcmsTempLateResolve(TempCurrLevelModelName, CurrLevelDataTable.Rows[j], NextLevelFileList, k + 1);
                                if (NextLavelFileArr[2].ToString().ToUpper() != TempCurrLevelModelName.ToUpper()) NextLevelStyleStr = this.DcmsTempLateResolve(TempCurrLevelModelName, CurrLevelDataTable.Rows[j], NextLevelStyleStr, k + 1);
                                LooPTempResolveHtml = this.ResolveHtmlModel(1, NextNextLeval, this.FormatStrProcess(NextNextLeval, 1), NextLevelStyleStr, NextLevelFileList, Controls);
                                TempCurrLevelStyleStr = TempCurrLevelStyleStr.Replace(string.Concat(new object[] { "{Dcms_Label_", NextNextLeval, ReplaceNextNextId, "}" }), LooPTempResolveHtml);
                                if (k == TempNextNextChildCount - 1) MyResolveHtmlSb.Append(TempCurrLevelStyleStr);
                            }
                        }
                        else
                        {
                            TempCurrLevelStyleStr = this.DcmsTempLateResolve(TempCurrLevelModelName, CurrLevelDataTable.Rows[j], TempCurrLevelStyleStr, j + 1);
                            MyResolveHtmlSb.Append(TempCurrLevelStyleStr);
                        }
                    }
                }
                else
                    return "";
            }
            return MyResolveHtmlSb.ToString();
        }
    }
}

